package com.aidex.biz.controller;

import java.util.List;

import com.aidex.common.annotation.Log;
import com.aidex.common.core.domain.R;
import com.aidex.common.utils.StringUtils;
import com.github.pagehelper.PageInfo;
import com.aidex.common.core.page.PageDomain;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.poi.ExcelUtil;

import javax.validation.constraints.*;

import org.springframework.web.bind.annotation.*;
import com.aidex.common.core.controller.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.aidex.biz.domain.CustomerFile;
import com.aidex.biz.service.CustomerFileService;
import com.aidex.common.utils.SecurityUtils;

/**
 * 客户附件Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2023-11-28
 */
@RestController
@RequestMapping("/biz/bizCustomerFile")
public class CustomerFileController extends BaseController {
    @Autowired
    private CustomerFileService customerFileService;

    /**
     * 查询客户附件列表
     */

    @GetMapping("/list")
    public R<PageInfo> list(CustomerFile customerFile, HttpServletRequest request, HttpServletResponse response) {
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            customerFile.setCompanyId(null);
        } else {
            customerFile.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        customerFile.setPage(new PageDomain(request, response));
        return R.data(customerFileService.findPage(customerFile));
    }

    /**
     * 获取客户附件详细信息
     */

    @GetMapping(value = "/{id}")
    public R<CustomerFile> detail(@PathVariable("id") String id) {
        return R.data(customerFileService.get(id));
    }

    /**
     * 新增客户附件
     */

    @Log(title = "客户附件", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated CustomerFile customerFile) {
        if(StringUtils.isEmpty(customerFile.getCustomerId())||StringUtils.isEmpty(customerFile.getType())){
            return R.fail("类型或者客户ID为空");
        }
        return R.status(customerFileService.save(customerFile));
    }

    /**
     * 修改客户附件
     */

    @Log(title = "客户附件", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated CustomerFile customerFile) {
        return R.status(customerFileService.save(customerFile));
    }


    /**
     * 删除客户附件
     */

    @Log(title = "客户附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(customerFileService.deleteCustomerFileByIds(ids));
    }

    /**
     * 获取最大编号
     */

    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(customerFileService.findMaxSort(new CustomerFile()));
    }

    /**
     * 导出客户附件列表
     */

    @Log(title = "客户附件", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(CustomerFile customerFile) {

        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            customerFile.setCompanyId(null);
        } else {
            customerFile.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<CustomerFile> list = customerFileService.findList(customerFile);
        ExcelUtil<CustomerFile> util = new ExcelUtil<CustomerFile>(CustomerFile.class);
        return util.exportExcel(list, "客户附件数据");
    }

}
